package org.example.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.RoleMenu;
import org.example.entity.TbMenu;
import org.example.service.IRoleMenuService;
import org.example.service.ITbMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaowenting
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/tb-menu")
public class TbMenuController {
    @Autowired
    IRoleMenuService iRoleMenuService;
    @Autowired
    ITbMenuService iTbMenuService;
    @RequestMapping("menuList")
    public List<Tree<String>> menuList(Integer userId){
        List<TbMenu> list = iTbMenuService.listAll(userId);
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("orders");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setNameKey("name");
// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPid());
                    tree.setWeight(treeNode.getOrders());
                    tree.setName(treeNode.getName());
                    tree.putExtra("url",treeNode.getUrl());
                    // 扩展属性 ...
                });
        return treeNodes;
    }

    /**
     * 添加菜单
     * @param tbMenu
     * @return
     */
    @RequestMapping("saveOrUpdateMenu")
    public String addMenu(@RequestBody TbMenu tbMenu){
        iTbMenuService.saveOrUpdate(tbMenu);
        return "success";
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("handleDelete")
    public String handleDelete(Integer id){
        TbMenu tbMenu = iTbMenuService.getById(id);
        if(Integer.parseInt(tbMenu.getPid())==0){
            Integer id1 = tbMenu.getId();
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("pid",id1);
            final List<TbMenu> list = iTbMenuService.list(queryWrapper);
            for (TbMenu o : list) {
                iTbMenuService.removeById(o.getId());
                LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                roleMenuLambdaQueryWrapper.eq(RoleMenu::getMenuId,o.getId());
                iRoleMenuService.remove(roleMenuLambdaQueryWrapper);
                LambdaQueryWrapper<RoleMenu> qe = new LambdaQueryWrapper<>();
                qe.eq(RoleMenu::getMenuId,o.getId());
                iRoleMenuService.remove(qe);
            }
            iTbMenuService.removeById(id);
            LambdaQueryWrapper<RoleMenu> qe = new LambdaQueryWrapper<>();
            qe.eq(RoleMenu::getMenuId,id);
            iRoleMenuService.remove(qe);
            return "success";
        }
        iTbMenuService.removeById(id);
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getMenuId,id);
        iRoleMenuService.remove(roleMenuLambdaQueryWrapper);
        return "success";
    }

    @RequestMapping("addFatherMenu")
    public String addFatherMenu(@RequestBody TbMenu tbMenu){
        System.out.println("前端传过来的父菜单对象"+tbMenu);
        tbMenu.setPid(0+"");
        iTbMenuService.save(tbMenu);
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(1);
        roleMenu.setMenuId(tbMenu.getId());
        iRoleMenuService.save(roleMenu);
        return "success";
    }

}
